﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Stock
{
    /// <summary>
    /// Sina Stock
    /// </summary>
    class SinaStock : IStock
    {
        /// <summary>
        /// 请求网页，返回结果以字符串返回
        /// </summary>
        /// <returns></returns>
        public string GetShareInfo(string stockList)
        {
            try
            {
                string url = "https://hq.sinajs.cn/list=";
                WebRequest.DefaultWebProxy = null;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + stockList);
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0";
                request.Referer = "https://finance.sina.com.cn";
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("gb2312"));
                string responseString = sr.ReadToEnd();
                sr.Close();
                stream.Close();
                return responseString;
            }
            catch (WebException e)
            {
                return "发现错误：" + e.Message;
            }
        }

        /// <summary>
        /// 处理股票信息
        /// </summary>
        public void PrintStock(string stockList)
        {
            StringBuilder sb = new StringBuilder();
            string response = GetShareInfo(stockList);
            string[] groupShares = response.Split(';');

            for (int i = 0; i < groupShares.Length; i++)
            {
                string singleShares = groupShares[i];

                if (!singleShares.Contains(','))
                {
                    continue;
                }

                string[] array = singleShares.Split(',');

                try
                {
                    double buyNum1 = Convert.ToDouble(array[10]) / 100;
                    double buyNum2 = Convert.ToDouble(array[12]) / 100;
                    double buyNum3 = Convert.ToDouble(array[14]) / 100;
                    double buyNum4 = Convert.ToDouble(array[16]) / 100;
                    double buyNum5 = Convert.ToDouble(array[18]) / 100;
                    double saleNum1 = Convert.ToDouble(array[20]) / 100;
                    double saleNum2 = Convert.ToDouble(array[22]) / 100;
                    double saleNum3 = Convert.ToDouble(array[24]) / 100;
                    double saleNum4 = Convert.ToDouble(array[26]) / 100;
                    double saleNum5 = Convert.ToDouble(array[28]) / 100;
                    double currentPrice = Convert.ToDouble(array[3]);
                    double yestodayPrice = Convert.ToDouble(array[2]);
                    string sharesCode = array[0].Substring(array[0].IndexOf('=') - 6, 6);

                    sb.AppendFormat("名称：{0}({1}){2}", array[0].Remove(0, 21).Replace('"', ' '), sharesCode, Environment.NewLine);
                    sb.AppendFormat("当前价格：{0}{1}", array[3], Environment.NewLine);
                    sb.AppendFormat("涨幅：{0}%{1}", Math.Round((currentPrice - yestodayPrice) / yestodayPrice, 4) * 100, Environment.NewLine);
                    sb.AppendFormat("涨跌：{0}{1}", Math.Round((currentPrice - yestodayPrice), 2), Environment.NewLine);
                    sb.AppendFormat("今日开盘价：{0}{1}", array[1], Environment.NewLine);
                    sb.AppendFormat("昨日收盘价：{0}{1}", array[2], Environment.NewLine);
                    sb.AppendFormat("今日最高价：{0}{1}", array[4], Environment.NewLine);
                    sb.AppendFormat("今日最低价：{0}{1}", array[5], Environment.NewLine);

                    if (sharesCode != "sh000001" && !sharesCode.Contains("399006"))//上证指数、创业板指不显示买一...
                    {
                        sb.AppendFormat("买一：{0}元 {1,8}手   卖一：{2}元 {3}手{4}", array[11], buyNum1, array[21], saleNum1, Environment.NewLine);
                        sb.AppendFormat("买二：{0}元 {1,8}手   卖二：{2}元 {3}手{4}", array[13], buyNum2, array[23], saleNum2, Environment.NewLine);
                        sb.AppendFormat("买三：{0}元 {1,8}手   卖三：{2}元 {3}手{4}", array[15], buyNum3, array[25], saleNum3, Environment.NewLine);
                        sb.AppendFormat("买四：{0}元 {1,8}手   卖四：{2}元 {3}手{4}", array[17], buyNum4, array[27], saleNum4, Environment.NewLine);
                        sb.AppendFormat("买五：{0}元 {1,8}手   卖五：{2}元 {3}手{4}", array[19], buyNum5, array[29], saleNum5, Environment.NewLine);
                    }

                    sb.AppendFormat("时间：{0}{1}", array[31], Environment.NewLine);
                    sb.AppendFormat("{0}{1}", "------------------------------------------------", Environment.NewLine);
                    Console.WriteLine(sb.ToString());
                    sb.Clear();
                }
                catch (Exception e)
                {
                    Console.WriteLine("未成功连接网络，正在重试...");
                }
            }
        }
    }
}
